/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package strategies;

import dao.DepartmentDAO;
import exceptions.DatabaseException;
import general.IContext;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import wrappers.DepartmentWrap;
import wrappers.EmployeeWrap;

/**
 *
 * @author KAV
 */
public class GroupEmployeesEditStrategy extends AbstractStrategy {

    //protected boolean isEditAllowed;
    protected Integer day = 0, month = 0, year = 0, editId;
    protected Object e;

    @Override
    public void executeOperation(IContext context) {
        prepareContext(context);
        e = null;
        op.echo("<div id=\"control\">");
        op.echo("<p>Select department</p>");
        //op.putSubmit("Back", "back");
        String tableSelection = "";
        if (pp.hasParam("table-selection"))
            tableSelection = "?table-selection=" + pp.getParam("table-selection");
        op.echo("<a href=\"index.jsp" + tableSelection + "\">Back</a>");
        op.echo("<a href=\"help.html\">Help</a>");
        if (dao != null) {
            List<Integer> groupEditIds = new LinkedList<Integer>();
            Set<String> keys = pp.getParamMap().keySet();
            for (String s : keys) {
                if (s.startsWith("row_")) {
                    int newId = Integer.parseInt(s.replace("row_", ""));
                    if (pp.getParamMap().get(s).compareTo("on") == 0) {
                        groupEditIds.add(newId);
                    }
                }
            }
            if (groupEditIds.isEmpty()) {
                op.putErrorReport("No rows selected", "");
            } else {
                try {
                    op.putSubmit("Change department", "group_edit_row");
                    op.echo("<div style=\"background: #eee\"></div>");
                    op.putBlockLabel("edit_department", "To department");
                    op.echo("<select class=\"inline\" id=\"edit_department\" name=\"edit_department\" size=\"1\">");
                    op.echo("<div style=\"background: #eee\"></div>");
                    DepartmentDAO dao2 = new DepartmentDAO();
                    List<Object> list = dao2.findAll();
                    EmployeeWrap ew = new EmployeeWrap();
                    for (Object o : list) {
                        DepartmentWrap obj = (DepartmentWrap) o;
                        op.putSelectOption(obj.getViewId().getValue(), obj.getViewTitle().getValue(), ew.getDepartment() == Integer.parseInt(obj.getViewId().getValue()));
                    }
                    op.echo("</select><div style=\"background: #eee\"></div>");
                    op.echo("</div>");
                    op.echo("<div>");
                    boolean flag = true;
                    op.echo("<table>");
                    op.startTableRow("head");
                    op.echo("<th>Employee</th><th>Id of employee</th>");
                    op.endTableRow();
                    for (int id : groupEditIds) {
                        ew = (EmployeeWrap) dao.findById(id);
                        op.startTableRow((flag = !flag) ? "even" : "odd");
                        op.putTableCell(ew.getName() + " " + ew.getSurname());
                        op.putTableTextCell(id + "", flag);
                        op.endTableRow();
                    }
                    op.echo("</table>");
                    op.echo("</div>");
                } catch (DatabaseException ex) {
                    Logger.getLogger(GroupEmployeesEditStrategy.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            op.echo("<input type=\"hidden\" name=\"table-selection\" id=\"table-selection\" value=\"" + pp.getParam("table-selection") + "\" />");

        }
    }
}
